﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SunshineAirlines
{
    public partial class FoodServices : Form
    {
        public FoodServices()
        {
            InitializeComponent();

        }
        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "select CONCAT( FlightNumber,',',DepartureAirportIATA+'-'+ArrivalAirportIATA,',',Date,',',Time,',',CabinTypeName) as 'Flight' from FlightReservation, CabinType, Schedule, route where IDTypeNumber = '" + textBox1.Text.Trim() + "' and FlightReservation.CabinTypeId in(2,3)  and Route.RouteId = Schedule.RouteId and FlightReservation.ScheduleId = Schedule.ScheduleId and CabinType.CabinTypeId = FlightReservation.CabinTypeId";
            comboBox2.DataSource = DBHelper.ExecuteTable(sql);
            comboBox2.DisplayMember = "Flight";
        }

        private void FoodServices_Load(object sender, EventArgs e)
        {
            //combox1设置ID Type
            string sql = "select distinct IDType from FlightReservation";
            comboBox1.DataSource = DBHelper.ExecuteTable(sql);
            comboBox1.DisplayMember = "IDType";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sql = "select * from FlightFood";
            if (flowLayoutPanel1.Controls.Count == 0)
            {//添加控件
                for (int i = 0; i < DBHelper.ExecuteTable(sql).Rows.Count; i++)
                {
                    UserControl1 f1 = new UserControl1(this);
                    //先更改用户控件中的控件属性,图片丢在debug
                    f1.Name = DBHelper.ExecuteTable(sql).Rows[i]["FoodId"].ToString();
                    f1.label1.Text = DBHelper.ExecuteTable(sql).Rows[i]["Name"].ToString();
                    f1.pictureBox1.Load("" + DBHelper.ExecuteTable(sql).Rows[i]["Img"].ToString() + "");
                    flowLayoutPanel1.Controls.Add(f1);
                }
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string sql = "select ReservationId from FlightReservation where IDTypeNumber='" + textBox1.Text.Trim() + "'";
            int a = (int)DBHelper.ExecuteScalar(sql);
            foreach (int key in DBHelper.foodAmount.Keys)
            {
                if (DBHelper.foodAmount[key] > 0)
                {
                    double val = DBHelper.foodAmount[key];
                    string querystr = " select count(0) from FlightFoodReservation ";
                    string wherestr = string.Format(" where ReservationId={0} and FoodId={1} ", a, key);
                    if ((int)DBHelper.ExecuteScalar(querystr + wherestr) > 0)
                    {
                        // update
                        string sql1 = "update FlightFoodReservation set Amount=" + val + wherestr;
                        DBHelper.ExecuteScalar(sql1);
                    }
                    else
                    {
                        //insert
                        string sql1 = string.Format(" insert into FlightFoodReservation(ReservationId,FoodId,Amount) values({0},{1},{2}) ", a, key, val);
                        DBHelper.ExecuteScalar(sql1);
                    }
                }
            }
            //foreach (int i in DBHelper.fooid)
            //{
            //    //if (DBHelper.fooid[i] != 0)
            //    if (DBHelper.foodAmount.ContainsKey(i) && DBHelper.foodAmount[i] > 0)
            //    {
            //        // 查 [ReservationId,FoodId]的组合是否存在
            //        // count(0) > 0  ->存在-> update; =0 -> 不存在, insert
            //        string querystr = " select count(0) from FlightFoodReservation ";
            //        string wherestr = string.Format(" where ReservationId={0} and FoodId={1} ", a, DBHelper.fooid[i]);
            //        if ((int)DBHelper.ExecuteScalar(querystr + wherestr) > 0)
            //        {
            //            // update
            //            string sql1 = "update FlightFoodReservation set Amount=" + DBHelper.foodAmount[i] + wherestr;
            //            DBHelper.ExecuteScalar(sql1);
            //        }
            //        else
            //        {
            //            //insert
            //            string sql1 = "insert into FlightFoodReservation(ReservationId,FoodId,Amount) values('" + a.ToString() + "','" + DBHelper.fooid[i].ToString() + "','" + DBHelper.foodAmount[i] + "')";
            //            DBHelper.ExecuteScalar(sql1);
            //        }

            //    }
            //}
        }



    }
}
